<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src\cmpage\model\base.js</title>
  
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="stylesheet" href="../assets/vendor/bootstrap/css/bootstrap.min.css">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
</head>
<body class="yui3-smart">
 <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
               <a class="navbar-brand mainlogo" href="/static/doc/index.html">
             
            <img alt="" src="../assets/css/logo.png" title="">
            
                
          </a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                 <ul class="nav navbar-nav">
                    
                    <li><a href="/home/index/index">首页</a>
                    </li>
                    
                    <li><a href="/static/doc/index.html">文档</a>
                    </li>
                    
                    <li><a href="/admin/index/index">演示</a>
                    </li>
                    
                    <li><a href="/home/index/log">日志</a>
                    </li>
                    
                </ul>
               <div class="navbar-form navbar-right filterAPi" autocomplete="off">
                <input type="text" id='txtSearchAPI' class="form-control search-query" placeholder="Search for API" />
                 <ul id="filterList" class="filterItems dropdown-menu" role="menu"></ul>
                </div>
            </div>
        </div>
    </nav>
    <div id="sidebar">
    <h3>Modules/Classes</h3>
        <div id="api-tabview-filter">
            <input id='txtSearch' type="search" class="form-control" placeholder="Type to filter Modules/Classes">
        </div>
        <dl id="sidebar_list">
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/admin.controller.html">admin.controller</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/admin.controller.base.html">admin.controller.base</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.controller.code.html">admin.controller.code</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.controller.index.html">admin.controller.index</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.controller.mob.html">admin.controller.mob</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/admin.model.html">admin.model</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/admin.model.code.html">admin.model.code</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.code_list.html">admin.model.code_list</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.code_lookup.html">admin.model.code_lookup</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.groupuser.html">admin.model.groupuser</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.groupuser_add.html">admin.model.groupuser_add</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.log.html">admin.model.log</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.login.html">admin.model.login</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.privilege.html">admin.model.privilege</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.teamuser.html">admin.model.teamuser</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.teamuser_add.html">admin.model.teamuser_add</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/admin.model.user.html">admin.model.user</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/cmpage.controller.html">cmpage.controller</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/cmpage.controller.base.html">cmpage.controller.base</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.mob.html">cmpage.controller.mob</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.module.html">cmpage.controller.module</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.page.html">cmpage.controller.page</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.controller.utils.html">cmpage.controller.utils</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/cmpage.logic.html">cmpage.logic</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/cmpage.logic.page.html">cmpage.logic.page</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/cmpage.model.html">cmpage.model</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/cmpage.model.area.html">cmpage.model.area</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.base.html">cmpage.model.base</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.file_list.html">cmpage.model.file_list</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page.html">cmpage.model.page</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page_excel.html">cmpage.model.page_excel</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page_lookup.html">cmpage.model.page_lookup</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.page_mob.html">cmpage.model.page_mob</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.utils.html">cmpage.model.utils</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.cmpage_global.html">cmpage.cmpage_global</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.cmpage_global_flow.html">cmpage.cmpage_global_flow</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/demo.model.html">demo.model</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/flow.model.proc_assign.html">flow.model.proc_assign</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/cmpage.model.appr.html">cmpage.model.appr</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.model.proc.html">flow.model.proc</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.model.task.html">flow.model.task</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.model.task_act.html">flow.model.task_act</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.model.task_act_appr.html">flow.model.task_act_appr</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/flow.controller.html">flow.controller</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/flow.controller.act.html">flow.controller.act</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.controller.base.html">flow.controller.base</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.controller.proc.html">flow.controller.proc</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.controller.task.html">flow.controller.task</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.controller.task_act.html">flow.controller.task_act</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
                <dt>
                    <span class="glyphicon glyphicon-minus"></span>
                    <a href="../modules/flow.model.html">flow.model</a>
                </dt>
                <dd>
                    <ul>
                   
                       <li>
                            
                           <a href="../classes/flow.model.act.html">flow.model.act</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                       <li>
                            
                           <a href="../classes/flow.model.act_path.html">flow.model.act_path</a>
                           <ul>
                                
                           </ul>
                       </li>
                    
                    </ul>
                </dd>
             
        </dl>
</div>
   
    <div class="stdoc-content">
        <!--     <form id="options-form" class="form-inline pull-right">
        Show:
        <label for="api-show-inherited" class="checkbox">
            <input type="checkbox" id="api-show-inherited" checked>
            Inherited
        </label>

        <label for="api-show-protected" class="checkbox">
            <input type="checkbox" id="api-show-protected">
            Protected
        </label>

        <label for="api-show-private" class="checkbox">
            <input type="checkbox" id="api-show-private">
            Private
        </label>
        <label for="api-show-deprecated" class="checkbox">
            <input type="checkbox" id="api-show-deprecated">
            Deprecated
        </label>

    </form>

 -->
        <div class="apidocs">
            <div id="docs-main">
                <div class="content">
                    <div class="page-header">
    <h1>src\cmpage\model\base.js <small>File</small></h1>
</div>

<div class="file">
    <pre class="prettyprint linenums" id='src_code'>
&#x27;use strict&#x27;;
// +----------------------------------------------------------------------
// | CmPage [ 通用页面框架 ]
// +----------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0
// +----------------------------------------------------------------------
// | Author: defans &lt;defans@sina.cn&gt;
// +----------------------------------------------------------------------

/**
 @module cmpage.model
 */

/**
 * 业务模块的基类，通过ORM框架 Sequelize 连接各个业务数据库&lt;/br&gt;
 * 连接参数配置于各个module的配置文件中，xxxx/config/db.config &lt;/br&gt;
 * 为了和thinkjs.model的常用方法名保持一致，以下实现了相关方法，通过转换成sql语句用 sequelize.query(sql) 执行 &lt;/br&gt;
 * 如果想使用 sequelize 的ORM方法，可以在子类中通过 this.sequelize.xxxx 来调用&lt;/br&gt;
 * 子类中用think.model,或者cmpage.model来实例化其他类其他模块，本类中的 model 用于设置表名以便生成SQL语句，也为了兼容 thinkjs.model(表名).xxx 的调用方式 &lt;/br&gt;
 * 如果以后版本的 ThinkJS能支持mssql，以上兼容可以使得将 page.js 改回从 think.model.base 继承的时候不需要做太大改动 &lt;/br&gt;
 * @class cmpage.model.base
 */
import Sequelize from &#x27;Sequelize&#x27;;

export default class extends think.base {
     sequelize = null;
     _model = null; //thinkjs.model.base , 目前暂时不用
     _field = &#x27;&#x27;;
     _where = &#x27;&#x27;;
     _tableName = &#x27;&#x27;;
     pk = &#x27;id&#x27;;

     /**
      * constructor
      * @param  {[type]} name   [description]
      * @param  {Object} config [description]
      * @return {[type]}        [description]
      */
     constructor(name, config = {}) {
         super();
         if (think.isObject(name)) {
             config = name;
             name = &quot;&quot;;
         }
         this._tableName = name;
         this.config = think.parseConfig(config);
         //debug(this.config, &#x27;base.constructor - this.config&#x27;);
         //debug(this.name, &#x27;base.constructor - this.name&#x27;);
     }

     /**
      * 创建连接
      * @return {[type]} [description]
      */
     getConnection() {
         this.sequelize = new Sequelize( this.config.database, this.config.user,  this.config.password, {
             host:this.config.host || &quot;127.0.0.1&quot;,
             port:this.config.port || 3306,
             dialect: this.config.type || &#x27;mysql&#x27;,
             benchmark:true,
             logging:this.log,
             pool: {
                 max: 5,
                 min: 0,
                 idle: 10000
             }
         });
         return this.sequelize;
     }
     log(msg){
         cmpage.debug(msg,&#x27;SQL&#x27;);
     }

     setTableName(name){
         this._tableName = name;
         return this;
     }
     model(name){
         this.setTableName(name);

        //  if(this.config.type != &#x27;mssql&#x27; &amp;&amp; (name.indexOf(&#x27;t_&#x27;) ===0 || name.indexOf(&#x27;vw_&#x27;) ===0 || name.indexOf(&#x27;fw_&#x27;) ===0) ){
        //      this._model = new think.model.base(name,this.config);
        //  }
         return this;
     }
     setPk(pk){
         this.pk = pk;
         if(this._model)    this._model.pk = pk;
         return this;
     }
     field(fields){
         this._field = fields;
         if(this._model)    this._model.field(fields);
         return this;
     }

     where(where){
         if(this._model){
             this._model.where(where);
             return this;
         }
         this._where = &#x27;&#x27;;
         if(think.isObject(where)){
             let arr = [];
             for(let p in where){
                 arr.push(p+&#x27;=&#x27;+this.parseValue(where[p]));
             }
             if(arr.length &gt;0)  this._where = &#x60;where ${arr.join(&#x27; and &#x27;)}&#x60;;
         }else {
             this._where = &#x27;where &#x27;+ where;
         }
         return this;
     }

     /**
      * 执行原生SQL语句，取结果集返回
      * @return {array} 查询结果集
      * @param {string} sql
      * @param {object} options 参数设置
      */
     async query(sql,options) {
         if(this._model){
             return await this._model.query(sql);
         }
         if (!this.sequelize) {
             this.getConnection();
         }
         let list = await this.sequelize.query(sql,options);
         if(list.length &gt;0) return list[0];
         return [];
     }

     async select(){
         if(this._model){
             return await this._model.select();
         }
         let sql = &#x60;select ${think.isEmpty(this._field) ? &#x27;*&#x27;: this._field} from ${this._tableName} ${this._where} &#x60;;
         return await this.query(sql);
     }
     async find(){
         if(this._model){
             return await this._model.find();
         }
         let list = await this.select();
         if(list.length &gt;0) return list[0];
         return {};
     }
     async delete(){
         //为了避免误操作，条件语句不能为空，当然，可以用 where 1=1
         if(think.isEmpty(this._where))  return;

         if(this._model){
             return await this._model.delete();
         }
         let sql = &#x60;delete from ${this._tableName} ${this._where}&#x60;;
         let ret = await this.query(sql);
         //debug(ret,&#x27;base.delete - ret&#x27;);
     }
     async count(){
         if(this._model){
             return await this._model.count();
         }
         let sql = &#x60;select count(*) as cnt from ${this._tableName} ${this._where}&#x60;;
         let list = await this.query(sql);
         return list[0][&#x27;cnt&#x27;];
     }

     async add(rec){
         if(this._model){
             return await this._model.add(rec);
         }
         let values = [];
         let _field = [];
         for(let key in rec){
             if(/^c_\w+/.test(key) &amp;&amp; key !=this.pk) {
                 let val = rec[key];
                 val = this.parseValue(val);
                 values.push(val);
                 _field.push(key);
             }
         }
         let sql =  &#x60;INSERT INTO ${this._tableName}( ${_field.join(&#x27;,&#x27;)} ) VALUES( ${values.join(&#x27;,&#x27;)} ); &#x60;;
         let list = await this.query(sql);
         list = await this.query(&#x27;select @@IDENTITY as id;&#x27;);
    //     debug(list);
         if(list.length &gt;0){
            return list[0][&#x27;id&#x27;];
         }
         return 0;
     }
     async update(rec){
         if(this._model){
             return await this._model.update(rec);
         }
         let _field = [];
         for (let key in rec) {
             if (/^c_\w+/.test(key) &amp;&amp; key != this.pk) {
                 let val = rec[key];
                 val = this.parseValue(val);
                 _field.push(key + &#x27;=&#x27; + val);
             }
         }
         if(think.isEmpty(this._where))  this._where = &#x60; where ${this.pk}=${rec[this.pk]}&#x60;;
         let sql = &#x60;UPDATE ${this._tableName} SET ${_field.join(&#x27;,&#x27;)}  ${this._where}&#x60;;
         //debug(sql,&#x27;base.update - sql&#x27;);
         await this.query(sql);

     }

    parseValue(value){
        if (think.isString(value)) {
            value = &#x60;&#x27;${value.replace(/\&#x27;/g,&#x27;\\\&#x27;&#x27;)}&#x27;&#x60;;
        }else if(think.isArray(value)){
            if (/^exp$/.test(value[0])) {
                value = value[1];
            }else{
                value = value.map(item =&gt; this.parseValue(item));
            }
        }else if(think.isBoolean(value)){
            value = value ? &#x27;TRUE&#x27; : &#x27;FALSE&#x27;;
        }else if (value === null) {
            value = &#x27;null&#x27;;
        }
        return value;
    }

}

    </pre>
</div>

                </div>
            </div>
        </div>
    </div>
<a id="gotoTop" class='well well-small' href='#'>
    Top
</a>
<script src="../assets/vendor/jquery/jquery-1.8.2.min.js"></script>
<script src="../assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script src="../assets/js/config.js"></script>
<script src="../assets/js/doc.js"></script>
</body>
</html>
